<template>
  <el-card>
    <h2>罚款结算</h2>
    <el-form :inline="true" @submit.prevent>
      <el-form-item label="支付状态">
        <el-select v-model="search.payStatus" placeholder="全部" style="min-width: 120px;" @change="getList">
          <el-option :value="''" label="全部" />
          <el-option :value="0" label="未支付" />
          <el-option :value="1" label="已支付" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getList">查询</el-button>
        <el-button type="primary" @click="showAdd = true">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="fineId" label="罚款ID" width="70" />
      <el-table-column prop="recordId" label="借阅记录ID" />
      <el-table-column prop="overdueDays" label="逾期天数" />
      <el-table-column prop="fineAmount" label="罚款金额" />
      <el-table-column prop="payStatus" label="支付状态">
        <template #default="scope">
          <el-tag :type="scope.row.payStatus === 1 ? 'success' : 'danger'">
            {{ scope.row.payStatus === 1 ? '已支付' : '未支付' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="payTime" label="支付时间">
        <template #default="scope">
          {{ scope.row.payTime ? scope.row.payTime.slice(0, 10) : '' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" type="primary" @click="payRow(scope.row)" :disabled="scope.row.payStatus === 1">支付</el-button>
          <el-button size="small" type="danger" @click="delRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="page"
      :page-size="size"
      :total="total"
      @current-change="getList"
      layout="prev, pager, next"
      style="margin-top: 20px; text-align: right;"
    />
    <el-dialog v-model="showAdd" title="新增罚款记录">
      <el-form :model="form">
        <el-form-item label="借阅记录ID"><el-input v-model="form.recordId" /></el-form-item>
        <el-form-item label="逾期天数"><el-input v-model="form.overdueDays" /></el-form-item>
        <el-form-item label="罚款金额"><el-input v-model="form.fineAmount" /></el-form-item>
        <el-form-item label="支付状态">
          <el-select v-model="form.payStatus">
            <el-option :value="0" label="未支付" />
            <el-option :value="1" label="已支付" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作员ID"><el-input v-model="form.operatorId" /></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="addRow">确定</el-button>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const list = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)
const search = reactive({ payStatus: '' })
const showAdd = ref(false)
const form = reactive({})

const getList = async () => {
  const params = { page: page.value, size: size.value }
  if (search.payStatus !== '' && search.payStatus !== null && search.payStatus !== undefined) {
    params.payStatus = search.payStatus
  }
  const res = await axios.get('/api/fine/list', { params })
  list.value = res.data.records
  total.value = res.data.total
}
onMounted(getList)

const payRow = async (row) => {
  await axios.post('/api/fine/pay', { fineId: row.fineId })
  ElMessage.success('支付成功')
  getList()
}

const delRow = async (row) => {
  try {
    await axios.post('/api/fine/delete', { fineId: row.fineId })
    ElMessage.success('删除成功')
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '删除失败')
  }
}

const addRow = async () => {
  try {
    await axios.post('/api/fine/add', form)
    ElMessage.success('新增成功')
    showAdd.value = false
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '新增失败')
  }
}
</script> 